import { getDeptTree } from "@/services/system/user";
import { message, Tree } from "antd";
import { useState ,useEffect, Key} from "react";
import { DeptTreePropType } from "../type/DeptTreePropType";

const DeptTree:React.FC<DeptTreePropType>=(props)=>{
    const [treeData,setTreeData]=useState<any>([]);
    const fetchDeptList=async()=>{
        const hide=message.loading('正在查询');
        try{
            hide();
            const deptListResult=await getDeptTree({});
            setTreeData(deptListResult);
        }
        catch(e){
            hide();
            return false;
        }
    }
    useEffect(()=>{
        fetchDeptList();
    },[]);
    const onSelect=(selectedKeys:React.Key[],info:any)=>{
        console.log(info)
        props.onSelectByDept(info.node.key);
    }
    return(
        <Tree.DirectoryTree treeData={treeData} defaultExpandAll onSelect={onSelect}/>
    )
}
export default DeptTree;